export default {
  ADD_TO_CART (state, { id }) {
    let product = state.added.find(item => item.id == id)
    if (!product) {
      state.added.push({
        id,
        num: 1
      })
    } else {
      product.num++
    }
  },
  NUM_CHANGE (state, { id, value }) {
    let prod = state.added.find(item => item.id == id)
    if (prod) {
      prod.num = value
    }
  },
  DEL_PRODUCT (state, product) {
    const index = state.added.findIndex(item => item.id == product.id)
    state.added.splice(index, 1)
  },
  CLEAR (state) {
    state.added = []
  }
}
